package hospital;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.R;

public class hospital_oxquiz extends Activity{
	static int count = 0;				// 맞춘문제개수
	static boolean answer = false;		// 실제답체크변수
	int sec = 50;
	//private TimerTask second;
	AlertDialog.Builder nologin;
	AlertDialog.Builder choisox;
	AlertDialog.Builder answerok;
	TextView oxquiz_content_text;
	
	private SharedPreferences sharedPref;
	private SharedPreferences.Editor sharedEditor;
	
	public void check(){
		switch(count)
        {
        case 0:
        	oxquiz_content_text.setText("문제1");
        	answer = true;
        	break;
        case 1:
        	oxquiz_content_text.setText("문제2");
        	answer = true;
        	break;
        case 2:
        	oxquiz_content_text.setText("문제3");
        	answer = true;
        	break;
        case 3:
        	oxquiz_content_text.setText("문제4");
        	answer = false;
        	break;
        case 4:
        	oxquiz_content_text.setText("문제5");
        	answer = false;
        	break;
        }
        
	}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hospital_oxquiz);
        

		sharedPref = getSharedPreferences("pref",Activity.MODE_PRIVATE);
		sharedEditor = sharedPref.edit();
		
        items.quiz_user_selected = 0;
        
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);	//	화면 안꺼짐
        
        oxquiz_content_text = (TextView) findViewById(R.id.oxquiz_content_text);
        final ImageView time_bar = (ImageView) findViewById(R.id.oxquiz_time_bar);
        Animation time_an = AnimationUtils.loadAnimation(this, R.anim.time);
        time_bar.startAnimation(time_an);
        
        check();
        
        final Button o_btn = (Button) findViewById(R.id.o_btn);
        final Button x_btn = (Button) findViewById(R.id.x_btn);
        
        o_btn.setOnTouchListener(new View.OnTouchListener(){
        	boolean myanswer = true;
        	@Override
        		public boolean onTouch(View v, MotionEvent event) {
        		// TODO Auto-generated method stub
        		/*if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
        			// 누를때 액션
        			//o_btn.setBackgroundResource(R.drawable.main_btn_l_down);
        		}else*/ if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
        			// 땔때 액션
        			//hospitalinfo_btn.setBackgroundResource(R.drawable.main_btn_l);
        			if(answer == myanswer)
        				//정답
        			{
        				
        				Toast.makeText(hospital_oxquiz.this,"정답입니다.",Toast.LENGTH_SHORT).show();
        				count++;
        				if(count==5){//퀴즈 모든문제 풀이 완료
        					count=0;
        					
            				Calendar cal = new GregorianCalendar();
            				int Month = cal.get(Calendar.MONTH)+1;
            				int Day = cal.get(Calendar.DAY_OF_MONTH);
            				
            				sharedEditor.putInt("Month",Month);
            				sharedEditor.putInt("Day",Day);
            				sharedEditor.commit();
        				
							Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
							startActivityForResult(intent, 0);
							finish();
						}else{	//퀴즈 문제가 남았을 경우
							runOnUiThread(new Runnable(){
					            @Override
					             public void run() {
					            	answerok = new AlertDialog.Builder(hospital_oxquiz.this)
							        .setTitle("정답입니다!")
							        .setMessage(oxquiz_content_text.getText()+"의 설명")
							        .setIcon(R.drawable.icon)
							        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
					    				@Override
					    				public void onClick(DialogInterface dialog, int which) {
					    					// TODO Auto-generated method stub
					    					/*items.quiz_no = items.quiz_no+1;
											if(items.event_list.get(items.quiz_no).getQuiz_type() == 3){
												Intent intent = new Intent(tour_oxquiz.this, tour_error_picture_search.class);
												startActivityForResult(intent, 0);
												finish();
											}else if(items.event_list.get(items.quiz_no).getQuiz_type() == 1){*/
					    						check();
												Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
												startActivityForResult(intent, 0);
												finish();
					    				}
					    			});
							        
					             }
					        });
							answerok.show();
						}
       				}
       	        	else
       	        		//오답
       	        	{
       	        		Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
       					startActivityForResult(intent, 0);
       					finish();
       	        	}        				
       			}
       			return false;
       		}
       	});
        
        x_btn.setOnTouchListener(new View.OnTouchListener(){
        	boolean myanswer = false;
        	@Override
        		public boolean onTouch(View v, MotionEvent event) {
        		// TODO Auto-generated method stub
        		/*if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
        			// 누를때 액션
        			//o_btn.setBackgroundResource(R.drawable.main_btn_l_down);
        		}else*/ if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
        			// 땔때 액션
        			//hospitalinfo_btn.setBackgroundResource(R.drawable.main_btn_l);
        			if(answer == myanswer)
        				//정답
        			{
        				
        				Toast.makeText(hospital_oxquiz.this,"정답입니다.",Toast.LENGTH_SHORT).show();
        				count++;
        				if(count==5){//퀴즈 모든문제 풀이 완료
        					count=0;
        					Calendar cal = new GregorianCalendar();
            				int Month = cal.get(Calendar.MONTH)+1;
            				int Day = cal.get(Calendar.DAY_OF_MONTH);
            				
            				sharedEditor.putInt("Month",Month);
            				sharedEditor.putInt("Day",Day);
            				sharedEditor.commit();
							Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
							startActivityForResult(intent, 0);
							finish();
						}else{	//퀴즈 문제가 남았을 경우
							runOnUiThread(new Runnable(){
					            @Override
					             public void run() {
					            	answerok = new AlertDialog.Builder(hospital_oxquiz.this)
							        .setTitle("정답입니다!")
							        .setMessage(oxquiz_content_text.getText()+"의 설명")
							        .setIcon(R.drawable.icon)
							        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
					    				@Override
					    				public void onClick(DialogInterface dialog, int which) {
					    					// TODO Auto-generated method stub
					    					/*items.quiz_no = items.quiz_no+1;
											if(items.event_list.get(items.quiz_no).getQuiz_type() == 3){
												Intent intent = new Intent(tour_oxquiz.this, tour_error_picture_search.class);
												startActivityForResult(intent, 0);
												finish();
											}else if(items.event_list.get(items.quiz_no).getQuiz_type() == 1){*/
					    						check();
												Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
												startActivityForResult(intent, 0);
												finish();
					    				}
					    			});
							        
					             }
					        });
							answerok.show();
						}
       				}
       	        	else
       	        		//오답
       	        	{
       	        		Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
       					startActivityForResult(intent, 0);
       					finish();
       	        	}        				
       			}
       			return false;
       		}
       	});
        	
 
			
			
		
        
        
        /*second = new TimerTask() {
			public void run() {
				sec--;
				if(sec == -1 ){
					second.cancel();
					if(items.quiz_user_selected == 0){
						runOnUiThread(new Runnable(){
				            @Override
				             public void run() {
				            	items.oxquiz_dial_selected = false;
		    			        time_bar.clearAnimation();
		    			        
				            	choisox = new AlertDialog.Builder(hospital_oxquiz.this);
				            	choisox.setTitle(items.appname);
				            	choisox.setMessage("O 와 X 중에 선택하여 주세요. 4초의 추가 시간이 주어집니다");
				            	choisox.setIcon(R.drawable.icon);
				            	choisox.setPositiveButton(" O ", new DialogInterface.OnClickListener(){
				    				@Override
				    				public void onClick(DialogInterface dialog, int which) {
				    					// TODO Auto-generated method stub
				    					items.quiz_user_selected = 1;
				    					items.oxquiz_dial_selected = true;
				    					
				    					if(items.quiz_user_selected == items.quiz_answer){
				    						items.event_point_final += items.event_list.get(items.quiz_no).getQuiz_gift_point();
				    						if((items.quiz_no+1) == items.event_list.size()){//퀴즈 모든문제 풀이 완료
												Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
												startActivityForResult(intent, 0);
												finish();
											}else{	//퀴즈 문제가 남았을 경우
												answerok = new AlertDialog.Builder(hospital_oxquiz.this)
										        .setTitle("정답입니다!")
										        .setMessage(items.event_list.get(items.quiz_no).getQuiz_explanation())
										        .setIcon(R.drawable.icon)
										        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
								    				@Override
								    				public void onClick(DialogInterface dialog, int which) {
								    					// TODO Auto-generated method stub
								    					items.quiz_no = items.quiz_no+1;
														if(items.event_list.get(items.quiz_no).getQuiz_type() == 1 || items.event_list.get(items.quiz_no).getQuiz_type() == 4 || items.event_list.get(items.quiz_no).getQuiz_type() == 5){
															Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
															startActivityForResult(intent, 0);
															finish();
														}
								    				}
								    			});
										        answerok.show();
											}
											
										}else{	//틀렸을 경우
											Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
											startActivityForResult(intent, 0);
											finish();
										}
				    				}
				    			});
				            	choisox.setNeutralButton(" X ", new DialogInterface.OnClickListener(){
				    				@Override
				    				public void onClick(DialogInterface dialog, int which) {
				    					// TODO Auto-generated method stub
				    					items.quiz_user_selected = 2;
				    					items.oxquiz_dial_selected = true;
				    					
				    					if(items.quiz_user_selected == items.quiz_answer){
				    						items.event_point_final += items.event_list.get(items.quiz_no).getQuiz_gift_point();
				    						if((items.quiz_no+1) == items.event_list.size()){//퀴즈 모든문제 풀이 완료
												Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
												startActivityForResult(intent, 0);
												finish();
											}else{	//퀴즈 문제가 남았을 경우
												answerok = new AlertDialog.Builder(hospital_oxquiz.this)
										        .setTitle("정답입니다!")
										        .setMessage(items.event_list.get(items.quiz_no).getQuiz_explanation())
										        .setIcon(R.drawable.icon)
										        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
								    				@Override
								    				public void onClick(DialogInterface dialog, int which) {
								    					// TODO Auto-generated method stub
								    					items.quiz_no = items.quiz_no+1;
														if(items.event_list.get(items.quiz_no).getQuiz_type() == 1 || items.event_list.get(items.quiz_no).getQuiz_type() == 4 || items.event_list.get(items.quiz_no).getQuiz_type() == 5){
															Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
															startActivityForResult(intent, 0);
															finish();
														}
								    				}
								    			});
										        answerok.show();
											}
											
										}else{	//틀렸을 경우
											Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
											startActivityForResult(intent, 0);
											finish();
										}
				    				}
				    			});
				            	choisox.show();
				            	
				            	final Handler oxHandler = new Handler();
				            	oxHandler.postDelayed(new Runnable() {

									@Override
									public void run() {
										// TODO Auto-generated method stub
										if(!items.oxquiz_dial_selected){
									        Toast.makeText(hospital_oxquiz.this,"4초가 경과 되었네요 ㅠㅠ",Toast.LENGTH_SHORT).show();													
											Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
											startActivityForResult(intent, 0);
											finish();
										}												
									}

				            	}, 4000); 
				             }
				        });
					}else{
						if(items.quiz_user_selected == items.quiz_answer){	
    						items.event_point_final += items.event_list.get(items.quiz_no).getQuiz_gift_point();
							if((items.quiz_no+1) == items.event_list.size()){//퀴즈 모든문제 풀이 완료
								Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
								startActivityForResult(intent, 0);
								finish();
							}else{	//퀴즈 문제가 남았을 경우
								runOnUiThread(new Runnable(){
						            @Override
						             public void run() {
						            	answerok = new AlertDialog.Builder(hospital_oxquiz.this)
								        .setTitle("정답입니다!")
								        .setMessage(items.event_list.get(items.quiz_no).getQuiz_explanation())
								        .setIcon(R.drawable.icon)
								        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
						    				@Override
						    				public void onClick(DialogInterface dialog, int which) {
						    					// TODO Auto-generated method stub
						    					items.quiz_no = items.quiz_no+1;
												if(items.event_list.get(items.quiz_no).getQuiz_type() == 1){
													Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
													startActivityForResult(intent, 0);
													finish();
												}
						    				}
						    			});
								        answerok.show();
						             }
						        });
							}
							
						}else{	//틀렸을 경우
							Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
							startActivityForResult(intent, 0);
							finish();
						}
					}
				}
			}
		};
		Timer timer = new Timer();
		timer.schedule(second, 0, 1000);
		
        final Button o_btn = (Button) findViewById(R.id.o_btn);
        final Button x_btn = (Button) findViewById(R.id.x_btn);
        
        final Drawable alpha1 = o_btn.getBackground();
        final Drawable alpha2 = x_btn.getBackground();
        alpha1.setAlpha(50);
        alpha2.setAlpha(50);
        
        items.quiz_answer = items.event_list.get(items.quiz_no).getQuiz_answer();
    	oxquiz_content_text.setText("문제임");
    			//(items.quiz_no+1)+". "+items.event_list.get(items.quiz_no).getQuiz_content().toString());
    	
        o_btn.setOnTouchListener(new View.OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
					alpha1.setAlpha(200);
					alpha1.invalidateSelf();
				}else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
					alpha1.setAlpha(50);
					alpha1.invalidateSelf();
					items.quiz_user_selected = 1;
					second.cancel();
			        time_bar.clearAnimation();
					if(items.quiz_user_selected == items.quiz_answer){	
						items.event_point_final += items.event_list.get(items.quiz_no).getQuiz_gift_point();
						if((items.quiz_no+1) == items.event_list.size()){//퀴즈 모든문제 풀이 완료
							Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
							startActivityForResult(intent, 0);
							finish();
						}else{	//퀴즈 문제가 남았을 경우
							answerok = new AlertDialog.Builder(hospital_oxquiz.this)
					        .setTitle("정답입니다!")
					        .setMessage(items.event_list.get(items.quiz_no).getQuiz_explanation())
					        .setIcon(R.drawable.icon)
					        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
			    				@Override
			    				public void onClick(DialogInterface dialog, int which) {
			    					// TODO Auto-generated method stub
			    					items.quiz_no = items.quiz_no+1;
									if(items.event_list.get(items.quiz_no).getQuiz_type() == 1 || items.event_list.get(items.quiz_no).getQuiz_type() == 4 || items.event_list.get(items.quiz_no).getQuiz_type() == 5){
										Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
										startActivityForResult(intent, 0);
										finish();
									}
			    				}
			    			});
					        answerok.show();
						}
						
					}else{	//틀렸을 경우
						Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
						startActivityForResult(intent, 0);
						finish();
					}
				}
					return false;
			}
		});
        
        x_btn.setOnTouchListener(new View.OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
					alpha2.setAlpha(200);
					alpha2.invalidateSelf();
				}else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
					alpha2.setAlpha(50);
					alpha2.invalidateSelf();
					items.quiz_user_selected = 2;
					second.cancel();
			        time_bar.clearAnimation();
					
					if(items.quiz_user_selected == items.quiz_answer){	
						items.event_point_final += items.event_list.get(items.quiz_no).getQuiz_gift_point();
						if((items.quiz_no+1) == items.event_list.size()){//퀴즈 모든문제 풀이 완료
							Intent intent = new Intent(hospital_oxquiz.this, quiz_answer_ok.class);
							startActivityForResult(intent, 0);
							finish();
						}else{	//퀴즈 문제가 남았을 경우					        
					        answerok = new AlertDialog.Builder(hospital_oxquiz.this)
					        .setTitle("정답입니다!")
					        .setMessage(items.event_list.get(items.quiz_no).getQuiz_explanation())
					        .setIcon(R.drawable.icon)
					        .setPositiveButton("다음문제로 이동", new DialogInterface.OnClickListener(){
			    				@Override
			    				public void onClick(DialogInterface dialog, int which) {
			    					// TODO Auto-generated method stub
			    					items.quiz_no = items.quiz_no+1;
									if(items.event_list.get(items.quiz_no).getQuiz_type() == 1 || items.event_list.get(items.quiz_no).getQuiz_type() == 4 || items.event_list.get(items.quiz_no).getQuiz_type() == 5){
										Intent intent = new Intent(hospital_oxquiz.this, hospital_oxquiz.class);
										startActivityForResult(intent, 0);
										finish();
									}
			    				}
			    			});
					        answerok.show();
						}
						
					}else{	//틀렸을 경우
						Intent intent = new Intent(hospital_oxquiz.this, quiz_failure.class);
						startActivityForResult(intent, 0);
						finish();
					}
				}
					return false;
			}
		});*/
    }
    
	
	
	
	
    public boolean onKeyDown( int KeyCode, KeyEvent event ){
 		if( event.getAction() == KeyEvent.ACTION_DOWN ){
 			if( KeyCode == KeyEvent.KEYCODE_BACK ){
 				Intent intent = new Intent(hospital_oxquiz.this, hospital_main.class);
 				startActivityForResult(intent, 0);
 				finish();
 				
 				return false;
 			}
 		}
 		return super.onKeyDown(KeyCode, event);
 	}
}
